package cn.wgx.common.security.service;

import cn.wgx.common.security.client.config.entity.LoginAppUser;
import cn.wgx.common.security.feign.module.entuser.EntUserFeign;
import cn.wgx.common.security.feign.module.sysuser.SysUserFeign;
import cn.wgx.common.util.MD5Util;
import cn.wgx.common.util.StringUtils;
import cn.wgx.common.util.ValidatorUtil;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 支持多种用户类型登录
 */
@Service("userDetailsService")
public class MultiUserDetailService implements UserDetailsService {

    @Resource
    SysUserFeign sysUserFeign;

    @Resource
    EntUserFeign entUserFeign;

    public UserDetails loadUserByUsername(String username, String type) throws UsernameNotFoundException {
        if(StringUtils.isBlank(type)){
            return loadUserByUsername(username);
        }
//判断是否是企业登录
        if("entUser".equals(type)){
//        if("sysUser".equals(type)){
            LoginAppUser byUsername = entUserFeign.findByUsername(username);
            if(byUsername == null){
                throw new UsernameNotFoundException("");
            }
            return  byUsername;
        }

        throw new UsernameNotFoundException("");
    }

        @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        LoginAppUser loginAppUser = null;
        //添加判断是否是手机号,如果是则需要根据手机号搜索用户
        if(ValidatorUtil.checkPhone(username)){
            loginAppUser = sysUserFeign.findByMobile(username);
        }else{
            loginAppUser = sysUserFeign.findByUsername(username);
        }
        if (loginAppUser == null || loginAppUser.getUser_id() == null) {
            throw new UsernameNotFoundException("");
        }
        try {
            RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            String agent = request.getHeader("user-agent");
            String remoteHost = request.getRemoteHost();
            loginAppUser.setClientSecret(MD5Util.md5Hex(agent + remoteHost));
        }catch (Exception e){
            e.printStackTrace();
        }
        return loginAppUser;
//        Set<String> permissions = loginAppUser.getPermissions();
//        List<GrantedAuthority> grantedAuthorities = Lists.newArrayList();
//        // 声明用户授权
//        permissions.forEach(tbPermission -> {
//            if (StringUtils.isNotBlank(tbPermission)) {
//                GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(tbPermission);
//                grantedAuthorities.add(grantedAuthority);
//            }
//        });

//        return new org.springframework.security.core.userdetails.User(username, loginAppUser.getMima(), grantedAuthorities);
    }

}
